package com.whz.disruptor.handler;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.whz.disruptor.event.MessageEvent;
import lombok.Data;

import java.util.function.Consumer;

/**
 * 生产者生产消息
 *
 * @author whz
 */
@Data
public class ProductHandler implements Consumer<Object> {


    private EventTranslatorOneArg<MessageEvent, Object> translatorOneArg = (event, sequence, message) -> event.setMessage(message);
    /**
     * 内存无锁循环队列
     */
    private RingBuffer<MessageEvent> ringBuffer;

    public ProductHandler(RingBuffer<MessageEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    @Override
    public void accept(Object message) {
        ringBuffer.publishEvent(this.translatorOneArg, message);
    }

    public void accept(Object message, EventTranslatorOneArg<MessageEvent, Object> translatorOneArg) {
        ringBuffer.publishEvent(translatorOneArg, message);
    }
}